package com.tank.demo.chainOfResponsibility;

import com.tank.demo.abstractFactory.BaseBullet;
import com.tank.demo.abstractFactory.BaseGameObject;
import com.tank.demo.abstractFactory.BaseTank;
import com.tank.demo.facadeAndMediator.GameModel;
import com.tank.demo.utils.RectangleUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.awt.*;


public class TankAndBulletCollider implements Collider {
    @Override
    public boolean collide(BaseGameObject o1, BaseGameObject o2) {
        if (o1 instanceof BaseTank && o2 instanceof BaseBullet) {
            if (RectangleUtil.intersects(o1, o2) && ((BaseTank) o1).group!=((BaseBullet) o2).group) {
                ((BaseTank) o1).die();
                ((BaseBullet) o2).die();
                GameModel.getInstance().add(GameModel.gameAbstractFactory.createExplode(o1.x+o1.width/2,o1.y+o1.height/2));
            }
            return true;
        } else if (o2 instanceof BaseTank && o1 instanceof BaseBullet) {
            collide(o2, o1);
        }
        return false;
    }
}
